package com.minhua.jijiehao2.mainframe.mine.updatepasswordactivity.impl

import android.text.TextUtils
import com.minhua.jijiehao2.base.BaseMvpPresenter
import com.minhua.jijiehao2.bean.BaseBean
import com.minhua.jijiehao2.constants.DefineKey
import com.minhua.jijiehao2.http.CallBack
import com.minhua.jijiehao2.http.ParamsMap
import com.minhua.jijiehao2.mainframe.mine.updatepasswordactivity.imp.IUpdatePasswordPresenter
import com.minhua.jijiehao2.mainframe.mine.updatepasswordactivity.imp.IUpdatePasswordView
import com.minhua.jijiehao2.utils.InputUtils
import com.minhua.jijiehao2.utils.T
import com.minhua.jijiehao2.utils.encryption.MDUtil

class UpdatePasswordPresenter : BaseMvpPresenter<IUpdatePasswordView>(), IUpdatePasswordPresenter {

    private val mUpdatePasswordModel = UpdatePasswordModel()

    override fun updatePassword() {
        val password = v?.getPassword() ?: ""
        val newPassword = v?.getNewPassword() ?: ""
        val confirmPassword = v?.getConfirmPassword() ?: ""
        if (TextUtils.isEmpty(password)) {
            T.showShort("密码不能为空")
            return
        }
        if (TextUtils.isEmpty(newPassword)) {
            T.showShort("新密码不能为空")
            return
        }
        if (!InputUtils.checkPassword(password)) {
            T.showShort("密码由6~20位数字和字母组成")
            return
        }
        if (newPassword != confirmPassword) {
            T.showShort("两次输入新密码不一致")
            return
        }

        val map = ParamsMap()
        map.put(DefineKey.OLD_PASSWORD, MDUtil.encode(MDUtil.TYPE.MD5, password))
        map.put(DefineKey.NEW_PASSWORD, MDUtil.encode(MDUtil.TYPE.MD5, newPassword))
        mUpdatePasswordModel.updatePassword(map, object : CallBack<Any>() {
            override fun onSuccess(t: BaseBean<Any>) {
                v?.onUpdateSuccess()
            }
        })
    }
}